HitchGui{

	var sourceName, 
		parent, 
		mainWindow, 
		hitchTypesArea, 
		voiceNamesArea, 
		type, 
		voiceName, 
		w, 
		voiceNamesSelect, 
		typeSelect,
		typeNames,
		voiceNames;

	*new{|sourceName, parent|
		^super.newCopyArgs(sourceName, parent).init;
	}
	
	getType{
		^typeNames.at(typeSelect.value);	
	}
	
	getVoiceName{
		^voiceNames.at(voiceNamesSelect.value);
	}
	
	init{
		var o;
		w = mainWindow = SCWindow("Choose a Hitch Target", Rect(50,500,300,500));
		mainWindow.userCanClose_(true);
		mainWindow.front;
		mainWindow = FlowView(mainWindow, Rect(0,0,300,500));
		
		hitchTypesArea =  FlowView.new(mainWindow, 300@100);
		hitchTypesArea.background_(Color.white);
		
		o = SCStaticText(hitchTypesArea,300@20).string = 'Choose a connection type:';
		o.background_( RippleGuiConstants.getHeaderBarColor );
		
		this.addConnectionTypeButtons(hitchTypesArea);
		
		voiceNamesArea = FlowView.new(mainWindow, 300@100);
		voiceNamesArea.background_(Color.white);
		
		
		o = SCStaticText(voiceNamesArea,300@20).string = "Choose a voice to hitch to" + sourceName ;
		o.background_( RippleGuiConstants.getHeaderBarColor );

		this.addVoiceChooser(voiceNamesArea);
		
		this.addDoneButt(mainWindow);
		
	}
	
	addDoneButt{|view|
	
			var v;
			v = SCButton(view,120@20);
			v.states = [
				["CONNECT",Color.white,Color.black]
			];
			v.action = {|butt|
				parent.addConnection(sourceName, this.getVoiceName, this.getType.asString);
				w.close;
			};
	}
	
	
	
	addVoiceChooser{|w|
		
		voiceNames = parent.getActiveVoiceNames;
		("voiceNames " + voiceNames ).postln;
		
		this.removeCurrentName(voiceNames);
		voiceNames = this.removeHitcherNames(voiceNames);

		voiceNamesSelect = SCPopUpMenu(w,Rect(10,10,180,20));
		voiceNamesSelect.items = voiceNames;
		voiceNamesSelect.background_(Color.white);	
	}
	
	removeHitcherNames{|voiceNamesArg|
		var returnList;
	
		returnList = List.new;
	
		voiceNamesArg.do{|name|
			"sourceName, name".postln;
			(sourceName + name).postln;
			if(parent.isVoiceInMasterChain(name, sourceName) == false){
				returnList.add(name);
			}
		};
		
		("returnList" + returnList).postln;
		
		^returnList.asArray;
	}
	
	removeCurrentName{|voicNames|
			// remove source voice from the list so we don't get  voice targeting itself
		voiceNames.do{|name|
			if(name == sourceName){		
				voiceNames.remove(name);
			}
		};
	}

	addConnectionTypeButtons{|view|
		var types, sbs;
		
		types = AbstractConnection.subclasses;
		
		typeNames = Array.new(types.size);
		
		types.do{|type|
			typeNames.add(type.name);	
		};
		
		typeNames.postln;
		
		typeSelect = SCPopUpMenu(view,Rect(10,10,180,20));
		typeSelect.items = typeNames;
		typeSelect.background_(Color.white);
		
		
	}

}